/**
 * 营销目标管理
 */
define(['app', 'js/directive/listTree', 'js/filter/tree2Arr', 'autocomplete', 'pagination', 'editor'], function(app) {
    'use strict';

    app.controller('Goals', function($scope, dataFactoryCRM, $q) {
        $scope.user;
        $scope.sector;
        $scope.goal_type;
        $scope.target_type;
        $scope.arrSector;
        $scope.nowYear;
        $scope.recentYear = [];
        $scope.start;
        $scope.end;
        $scope.goals1;
        $scope.goals2;
        $scope.goals3;
        $scope.goals4;
        $scope.goalsRcode; //返回码
        $scope.goalsMsg; //返回错误信息

        $scope.goalsPage = {
            pageAll: 1,
            pageNow: 1,
            pageSize: $scope.psize,
            k: 4
        };
        /*系统错误调用*/
        $scope.msgAttention = function() {
            $('.ui.systemerror').modal({
                closable: true,
                onDeny: function() {
                    return true;
                },
                onApprove: function() {
                    $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
                }
            }).modal('show');

            setTimeout(function() {
                var system = $('.ui.dimmer.modals.page.transition.visible.active');
                if (system) {
                    $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
                }
            }, 1000);
        };
        /*获取登录用户信息*/
        $scope.userInfo = function() {
            /* 获取登陆者信息 */
            var obj = new Function("return" + sessionStorage.user)();
            $scope.userinfo = obj;

            /*get user sectors*/
            var _url = 'sector/index';
            dataFactoryCRM.getDataByAPI(_url).then(function(_d) {
                if (_d.rcode)
                    $scope.sectors = _d.data;
            });
        };
        /*获取当前时间*/
        $scope.GetNowDate = function() {
            var myDate, myDateStr;
            myDate = new Date();
            var yyyy = myDate.getFullYear(); //取四位年份
            $scope.nowYear = yyyy;
            for (var i = 0; i < 3; i++) {
                $scope.recentYear[i] = yyyy - 1 + i;
            }
            var MM = myDate.getMonth() + 1; //取月份
            if (MM < 10) {
                MM = "0" + MM;
            }
            var dd = myDate.getDate(); //取日  
            if (dd < 10) {
                dd = "0" + dd;
            }
            myDateStr = yyyy + "-" + MM + "-" + dd;
            return myDateStr;
        };
        $scope.getYear = function() {
            $scope.nowYear = this.nYear;
        };
        /*获取营销目标信息 不可操作：1；可修改：2；可添加：3；*/ 
        $scope.getGoals = function() {
            $scope.$emit("BUSY");
            if (!$scope.goal_type || $scope.goal_type == '') {
                $scope.goal_type = 1;
            }
            if (!$scope.target_type || $scope.target_type == '') {
                $scope.target_type = 2;
            }

            var url = 'Goals/index?p=' + $scope.goalsPage.pageNow + '&psize=' + $scope.goalsPage.pageSize + '&goal_type=' + $scope.goal_type + '&target_type=' + $scope.target_type + '&arrSector=' + $scope.arrSector + '&t_year=' + $scope.nowYear;
            dataFactoryCRM.getDataByAPI(url).then(function(_d) {
                if (_d.rcode) {
                    $scope.goalsPage.pageSize = _d.psize;
                    $scope.goalsPage.pageAll = _d.pall;
                    $scope.goalsPage.pageNow = _d.p;
                    var length1 = _d.data.length;
                    var nowTime = $scope.GetNowDate();
                    for (var i = 0; i < length1; i++) {
                        var length2 = _d.data[i]['data'].length;
                        for (var j = 0; j < length2; j++) {
                            if (nowTime > _d.data[i]['data'][j]['end_time']) {
                                _d.data[i]['data'][j]['content'] = 1;
                            } else {
                                if (_d.data[i]['data'][j]['goal'] && _d.data[i]['data'][j]['goal'] != '0.00') {
                                    _d.data[i]['data'][j]['content'] = 2;
                                } else {
                                    if (($scope.target_type == 1 && $scope.userinfo.user_id == _d.data[i]['data'][j]['target']) || ($scope.target_type == 2 && $scope.userinfo.sector_id == _d.data[i]['data'][j]['target'])) {
                                        _d.data[i]['data'][j]['content'] = 3;
                                    } else {
                                        _d.data[i]['data'][j]['content'] = 1;
                                    }
                                }
                            }
                        }
                    }
                    if ($scope.goal_type == 1) {
                        $scope.goals1 = _d.data;
                    } else if ($scope.goal_type == 2) {
                        $scope.goals2 = _d.data;
                    } else if ($scope.goal_type == 3) {
                        $scope.goals3 = _d.data;
                    } else if ($scope.goal_type == 4) {
                        $scope.goals4 = _d.data;
                    }
                } else {
                    $scope.goals1 = '';
                    $scope.goals2 = '';
                    $scope.goals3 = '';
                    $scope.goals4 = '';
                }
                $scope.$emit("NOTBUSY");
            });
        };
        /**
         * 切换营销目标类型 月：1 季度：2 半年度：3 年度：4  key = goal_type  value = target_type
         * 	$scope.goal_type		目标类型:1、月度目标；2、季度目标；3、半年度目标；4、年度目标
         *	$scope.target_type		个人1、部门2
         */
        $scope.switchType = function(key, value) {
            if (this.vitem) {
                var _vitem = this.vitem;
                $scope.arrSector = _vitem.sector_id;
            }
            if (value && value != $scope.target_type) {
                $scope.target_type = value;
                $scope.goalsPage.pageNow = 1;
            }
            if (key) {
                $scope.goal_type = key;
                $scope.getGoals();
            }
        };
        /*点击单元格事件(添加、修改营销目标)*/
        $scope.clickGoal = function() {
            var _vgoal = this.col;
            $scope.start = _vgoal.start_time;
            $scope.end = _vgoal.end_time;
            var nowTime = $scope.GetNowDate();

            if (nowTime <= _vgoal.end_time) {
                if (_vgoal.goal && _vgoal.goal != '0.00') {
                    //编辑
                    $('#frm-editGoal input[name="edit_goal"]').val(_vgoal.goal);
                    if (($scope.target_type == 1 && $scope.userinfo.user_id == _vgoal.target) || ($scope.target_type == 2 && $scope.userinfo.sector_id == _vgoal.target)) {
                        $('.ui .editGoal').modal({
                            onApprove: function() {
                                var da = $('#frm-editGoal').serialize() + '&goal_id=' + _vgoal.goal_id;
                                if ($('#frm-editGoal').form('is valid')) {
                                    $scope.editGoal(da);
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        }).modal('show');
                    }
                } else {
                    //添加
                    if (($scope.target_type == 1 && $scope.userinfo.user_id == _vgoal.target) || ($scope.target_type == 2 && $scope.userinfo.sector_id == _vgoal.target)) {
                        $('.ui .addGoal').modal({
                            onApprove: function() {
                                var da = $('#frm-addGoal').serialize() + '&start_time=' + _vgoal.start_time + '&end_time=' + _vgoal.end_time + '&goal_type=' + _vgoal.goal_type + '&target_type=' + _vgoal.target_type + '&target=' + _vgoal.target;
                                if ($('#frm-addGoal').form('is valid')) {
                                    $scope.addGoal(da);
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        }).modal('show');
                    }
                }
            } else {
                return false;
            }
        };
        /*修改Goal*/
        $scope.editGoal = function(da) {
            $scope.$emit("BUSY");
            $scope.busy = dataFactoryCRM.postDataByAPI('Goals/editGoal', da)
                .then(function(data) {
                    $scope.goalsRcode = data.rcode;
                    if (data.rcode) {
                        $scope.getGoals();
                    } else {
                        $scope.goalsMsg = data.msg;
                        $scope.msgAttention();
                        $scope.$emit("NOTBUSY");
                    }
                    $('#frm-editGoal input').val('');
                });
        };
        /*添加Goal*/
        $scope.addGoal = function(da) {
            $scope.$emit("BUSY");
            $scope.busy = dataFactoryCRM.postDataByAPI('Goals/addGoal', da)
                .then(function(data) {
                    $scope.goalsRcode = data.rcode;
                    if (data.rcode) {
                        $scope.getGoals();
                    } else {
                        $scope.$emit("NOTBUSY");
                        $scope.goalsMsg = data.msg;
                        $scope.msgAttention();
                    }
                    $('#frm-addGoal input').val('');
                });
        };
        /*自动加载页面*/
        $scope.adutoLoading = function() {
            $scope.userInfo();
            $scope.GetNowDate();
            $scope.getGoals();
        };
        $scope.adutoLoading();
    });
});